package com.demo.modules.job.task;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.demo.common.constant.ConfigConstant;
import com.demo.common.utils.CacheUtils;
import com.demo.modules.sys.entity.SysLogEntity;
import com.demo.modules.sys.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * 日志清除定时任务
 *
 * @author Zsx
 */
@Component("logTask")
@Slf4j
public class LogTask implements ITask {
    @Autowired
    private SysLogService sysLogService;

    @Override
    public void run(String params) {
        log.debug("logTask定时任务正在执行，参数为：{}", params);
        this.cleanLog(params);
    }

    private void cleanLog(String params) {
        int expireDay = Integer.parseInt(CacheUtils.getConfCache(ConfigConstant.LOG_EXPIRED).getParamValue());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -expireDay);
        String expireDateStr = DateUtil.formatDate(cal.getTime());
        sysLogService.remove(new LambdaQueryWrapper<SysLogEntity>().lt(SysLogEntity::getCreateDate, expireDateStr));
    }
}
